/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.sys.web.vote;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.sys.entity.vote.VoteCandidate;
import com.thinkgem.jeesite.modules.sys.service.vote.VoteCandidateService;

/**
 * 投票管理Controller
 * @author 郑学兵
 * @version 2017-05-22
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/vote/voteCandidate")
public class VoteCandidateController extends BaseController {

	@Autowired
	private VoteCandidateService voteCandidateService;
	
	@ModelAttribute
	public VoteCandidate get(@RequestParam(required=false) String id) {
		VoteCandidate entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = voteCandidateService.get(id);
		}
		if (entity == null){
			entity = new VoteCandidate();
		}
		return entity;
	}
	
	@RequiresPermissions("sys:vote:voteCandidate:view")
	@RequestMapping(value = {"list", ""})
	public String list(VoteCandidate voteCandidate, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<VoteCandidate> page = voteCandidateService.findPage(new Page<VoteCandidate>(request, response), voteCandidate); 
		model.addAttribute("page", page);
		return "modules/sys/vote/voteCandidateList";
	}

	@RequiresPermissions("sys:vote:voteCandidate:view")
	@RequestMapping(value = "form")
	public String form(VoteCandidate voteCandidate, Model model) {
		model.addAttribute("voteCandidate", voteCandidate);
		return "modules/sys/vote/voteCandidateForm";
	}

	@RequiresPermissions("sys:vote:voteCandidate:edit")
	@RequestMapping(value = "save")
	public String save(VoteCandidate voteCandidate, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, voteCandidate)){
			return form(voteCandidate, model);
		}
		voteCandidateService.save(voteCandidate);
		addMessage(redirectAttributes, "保存投票管理成功");
		return "redirect:"+Global.getAdminPath()+"/sys/vote/voteCandidate/?repage";
	}
	
	@RequiresPermissions("sys:vote:voteCandidate:edit")
	@RequestMapping(value = "delete")
	public String delete(VoteCandidate voteCandidate, RedirectAttributes redirectAttributes) {
		voteCandidateService.delete(voteCandidate);
		addMessage(redirectAttributes, "删除投票管理成功");
		return "redirect:"+Global.getAdminPath()+"/sys/vote/voteCandidate/?repage";
	}

}